import com.sun.source.tree.Tree;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    //当前搜索树的根节点
    public TreeNode root = null;
    /*public TreeNode search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val > val) {
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }*/
    public TreeNode search(int val) {
        //让cur指向根节点
        TreeNode cur = root;
        while (cur != null) {
            if (val > cur.val) {
                cur = cur.right;//val比根节点的值大，去右子树找
            } else if (val < cur.val) {
                cur = cur.left;//val比根节点的值小，去左子树找
            } else {
                return cur;//找到了
            }
        }
        return null;
    }

    //二叉搜索树的插入
    public void insert(int val) {
        TreeNode newNode = new TreeNode(val);
        if (root == null) {
            root = newNode;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            parent = cur;
            if (val > cur.val) {
                cur = cur.right;
            } else if (val < cur.val) {
                cur = cur.left;
            } else {
                return;//存在相同数值，不插入
            }
        }
        //走到这里，cur为空了，创建节点
        if (val > parent.val) {
            parent.right = newNode;
        } else {
            parent.left = newNode;
        }
    }


    /*public void insert(int val) {
        TreeNode newNode = new TreeNode(val);
        if (root == null) {
            root = newNode;
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            parent = cur;
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                //已有该数值
                return;
            }
        }
        //cur为空了
        if (parent.val < val) {
            parent.right = newNode;
        } else {
            parent.left = newNode;
        }
    }*/

    //二叉搜索树的删除
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        //查找到节点位置
        while (cur != null) {
            parent = cur;
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                //找到节点，进行删除
                removeNode(cur,parent);
                return;
            }
        }
    }

    private void removeNode(TreeNode cur, TreeNode parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            //去右子树找最小值节点
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            //最左边是最小值，所以要找到最左边
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            //交换值
            cur.val = target.val;
            if (target == targetParent.left) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }
}
